function createPoster(data, context, canvasInfo) {
  return new Promise(async (resolve, reject) => {
    const system = wx.getSystemInfoSync();
    const portrait = await downloadFile(data.portrait)
    const portraitInfo = await getImageInfo(portrait)
    if (data.wxYard) {
      const qrCode = await downloadFile(data.wxYard)
      const qrCodeInfo = await getImageInfo(qrCode)
      drawImage(context, 50, 50, qrCode, qrCodeInfo.width, qrCodeInfo.height, system.windowWidth - 100, 270)
    }

    context.rect(20, 162, system.windowWidth, 10)
    context.setFillStyle('#1576C6')
    context.fill()
    context.beginPath();
    drawImage(context, 120, 173, portrait, portraitInfo.width, portraitInfo.height, 12, 12)
    context.fillStyle = "#1576C6";
    context.setFontSize(20)
    context.fillText(data.name, 152, 47);
    // company
    context.fillStyle = "#333333";
    context.setFontSize(12)
    if (data.company) {
      context.fillText('杭州无电电子商务有限公司', 152, 72);
    }
    context.fillText(`手机: ${data.phone}`, 152, 112);
    if (data.mailbox) {
      context.fillText(`邮箱: ${data.mailbox}`, 152, 132);
    }
    context.fillText(`地址: ${data.site}`, 152, 152);

    context.fillText('您好,', 12, 220);
    context.fillText(`我是E单配渠道销售部总监 ${data.name}+${data.post || ''}`, 12, 240);
    context.fillText('保存海报至手机', 12, 260);
    context.fillText('记得来看看我的微信哦', 12, 280);

    context.rect(12, 300, 40, 5)
    context.setFillStyle('#1576C6')
    context.fill()
    context.beginPath();
    context.fillStyle = "#333333";
    context.fillText('长按二维码 保存名片到相册', 12, 325);
    context.stroke()
    context.save();
    context.draw();
    setTimeout(async () => {
      const filePath = await canvasToTempFilePath(canvasInfo.width, canvasInfo.height)
      resolve(filePath)
    }, 300)
  })
}

function downloadFile(url) {
  return new Promise(async (resolve, reject) => {
    wx.downloadFile({
      url: url,
      success: (res) => resolve(res.tempFilePath),
      fail: () => reject()
    })
  })
}
const getImageInfo = (netUrl) => {
  return new Promise((resolve, reject) => {
    wx.getImageInfo({
      src: netUrl,
      success: (res) => res.errMsg == "getImageInfo:ok" ? resolve(res) : reject(res),
      fail: (err) => reject(err)
    })
  })
}
function drawImage(ctx, bg_w, bg_h, imgPath, imgWidth, imgHeight, x, y) {
  let dWidth = bg_w / imgWidth;  // canvas与图片的宽度比例
  let dHeight = bg_h / imgHeight;  // canvas与图片的高度比例
  if (imgWidth > bg_w && imgHeight > bg_h || imgWidth < bg_w && imgHeight < bg_h) {
    if (dWidth > dHeight) {
      ctx.drawImage(imgPath, 0, (imgHeight - bg_h / dWidth) / 2 - 55, imgWidth, bg_h / dWidth, x, y, bg_w, bg_h)
    } else {
      ctx.drawImage(imgPath, (imgWidth - bg_w / dHeight) / 2, 0, bg_w / dHeight, imgHeight, x, y, bg_w, bg_h)
    }
  } else {
    if (imgWidth < bg_w) {
      ctx.drawImage(imgPath, 0, (imgHeight - bg_h / dWidth) / 2, imgWidth, bg_h / dWidth, x, y, bg_w, bg_h)
    } else {
      ctx.drawImage(imgPath, (imgWidth - bg_w / dHeight) / 2, 0, bg_w / dHeight, imgHeight, x, y, bg_w, bg_h)
    }
  }
}

const canvasToTempFilePath = (width, height) => {
  return new Promise((resolve, reject) => {
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      x: 0,
      y: 0,
      width: width,
      height: height,
      fileType: "jpg",
      quality: 1,
      success: (res) => resolve(res.tempFilePath),
      fail: (err) => reject(err)
    })
  })
}

module.exports = {
  createPoster
}